// Copyright (c) 2019 Presto Labs Pte. Ltd.
// Author: jaewon

#pragma once

#include <cstdint>
#include <string>
#include <string_view>
#include <unordered_map>

#include <nlohmann/json.hpp>

#include "coin1/fastfeed/view/base.h"

namespace fastfeed {
namespace view {

class JsonObjectTableBuilder : public BuilderBase {
 public:
  using TableType = std::unordered_map<std::string, nlohmann::json>;

  ~JsonObjectTableBuilder() final = default;

  bool UpdateByProto(const proto::Feed& feed) final;

  const TableType& table() const { return table_; }
  const nlohmann::json* GetObject(const std::string& key) const {
    auto it = table_.find(key);
    if (it == table_.end()) return nullptr;
    return &it->second;
  }

 private:
  void Insert(const proto::JsonObjectTableRow& row);
  void Update(const proto::JsonObjectTableRow& row);
  void Delete(const proto::JsonObjectTableRow& row);

  TableType table_;
};  // class JsonObjectTableBuilder

}  // namespace view
}  // namespace fastfeed
